//
//  SMAlertView.m
//  Demo
//
//  Created by btcc on 2021/7/1.
//

#import "SMAlertView.h"
#import <Masonry.h>

#define ConstScreenWidth            ([UIScreen mainScreen].bounds.size.width)
#define ConstScreenHeight           ([UIScreen mainScreen].bounds.size.height)

/// 间距
#define ConstSpaceWidth             (25)

typedef void(^CompleteBlock)(NSInteger currentIndex);

@interface SMAlertView ()
@property (nonatomic, strong) UIView *maskView; // 背景
@property (nonatomic, strong) UILabel *titleLabel; // 标题
@property (nonatomic, strong) UILabel *contentLabel; // 内容message
@property (nonatomic, strong) UIView *hDivideView; // 横向分割线
@property (nonatomic, strong) UIView *vDivideView; // 竖向分割线
@property (nonatomic, strong) UIButton *itemButton; // 按钮
@property (nonatomic, strong) NSMutableArray<UIButton *> *itemButtonsArray; // 按钮数组
@property (nonatomic,   copy) CompleteBlock completeBlock; // 回调
@end

@implementation SMAlertView

/// 弹窗初始化方法
/// @param title 标题
/// @param message 内容
/// @param actionsName 底部按钮名字数组
/// @param handle 回调方法
+ (instancetype)alertWithWithTitle:(NSString *)title message:(NSString *)message actions:(NSArray<NSString *> *)actionsName completeHandle:(void(^)(NSInteger currentIndex))handle {
    SMAlertView *alerView = [[SMAlertView alloc] init];
    alerView.completeBlock = handle;
    alerView.titleLabel.text = title;
    alerView.contentLabel.text = message;
    [alerView handleActions:actionsName];
    [alerView showAnimation];
    return alerView;
}

/// 默认初始化
- (instancetype)init {
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
        [self createUI];
        [self showAlertView];
    }
    return self;
}

#pragma mark -------------------- 处理数据 --------------------

- (void)handleActions:(NSArray<NSString *> *)actionsName {
    if (actionsName.count == 0) {
        [self.maskView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(ConstSpaceWidth);
        }];
        return;
    } else {
        [actionsName enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            self.itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.itemButton setTitle:obj forState:UIControlStateNormal];
            [self.itemButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
            [self.itemButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.itemButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
            self.itemButton.tag = idx;
            [self.maskView addSubview:self.itemButton];
            [self.itemButtonsArray addObject:self.itemButton];
        }];
        
        [self.maskView addSubview:self.hDivideView];
        [self.hDivideView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.maskView.mas_left);
            make.right.mas_equalTo(self.maskView.mas_right);
            make.top.mas_equalTo(self.contentLabel.mas_bottom).mas_offset(ConstSpaceWidth);
            make.height.mas_equalTo(1);
        }];
        
        if (self.itemButtonsArray.count == 1) {
            [self.itemButtonsArray mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.maskView.mas_left).mas_offset(ConstSpaceWidth);
                make.top.mas_equalTo(self.hDivideView.mas_bottom).mas_offset(10);
                make.right.mas_equalTo(self.maskView.mas_right).mas_offset(-ConstSpaceWidth);
                make.height.mas_equalTo(40);
            }];
        } else {
            [self.maskView addSubview:self.vDivideView];
            [self.vDivideView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(self.maskView.mas_centerX);
                make.top.mas_equalTo(self.hDivideView.mas_bottom).mas_offset(20);
                make.bottom.mas_equalTo(self.maskView.mas_bottom).mas_offset(-20);
                make.width.mas_equalTo(1);
            }];
            
            [self.itemButtonsArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
            [self.itemButtonsArray mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(self.hDivideView.mas_bottom).mas_offset(10);
                make.height.mas_equalTo(40);
            }];
        }
        
        [self.maskView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.itemButton.mas_bottom).mas_offset(10);
        }];
    }
}

// 显示
- (void)showAlertView {
    [[UIApplication sharedApplication].delegate.window addSubview:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (touches.anyObject.view == self) {
        [self removeFromSuperview];
    }
}

#pragma mark -------------------- 方法 --------------------

- (void)clickButton:(UIButton *)sender {
    if (self.completeBlock) {
        [self removeFromSuperview];
        self.completeBlock(sender.tag);
    }
}

#pragma mark -------------------- 显示动画 --------------------

- (void)showAnimation {
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basicAnimation.fromValue = @1;
    basicAnimation.toValue = @1.1;
    basicAnimation.repeatCount = 1;
    basicAnimation.duration = 0.1;
    basicAnimation.removedOnCompletion = YES;
    basicAnimation.autoreverses = YES;
    [self.maskView.layer addAnimation:basicAnimation forKey:@"alert"];
}

#pragma mark -------------------- UI --------------------

- (void)createUI {
    [self addSubview:self.maskView];
    [self.maskView addSubview:self.titleLabel];
    [self.maskView addSubview:self.contentLabel];
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.width.mas_equalTo(ConstScreenWidth - (ConstSpaceWidth * 2));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.maskView.mas_top).mas_offset(ConstSpaceWidth);
        make.centerX.mas_equalTo(self.maskView.mas_centerX);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(ConstSpaceWidth);
        make.left.mas_equalTo(self.maskView.mas_left).mas_offset(ConstSpaceWidth);
        make.right.mas_equalTo(self.maskView.mas_right).mas_offset(-ConstSpaceWidth);
    }];
}

#pragma mark -------------------- lazy --------------------

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = [UIColor whiteColor];
        _maskView.layer.cornerRadius = 8;
    }
    return _maskView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
    }
    return _titleLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UIView *)hDivideView {
    if (!_hDivideView) {
        _hDivideView = [[UIView alloc] init];
        _hDivideView.backgroundColor = [UIColor orangeColor];
    }
    return _hDivideView;
}

- (UIView *)vDivideView {
    if (!_vDivideView) {
        _vDivideView = [[UIView alloc] init];
        _vDivideView.backgroundColor = [UIColor orangeColor];
    }
    return _vDivideView;
}

- (NSMutableArray<UIButton *> *)itemButtonsArray {
    if (!_itemButtonsArray) {
        _itemButtonsArray = [NSMutableArray array];
    }
    return _itemButtonsArray;
}

@end
